﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a simple corner detection i created on my own.
    /// </summary>
    public class Corner : BaseForeBackgroundColorFilter, ICornerFinder
    {
        protected int m_nThreshold = 10;
        protected bool m_bMax = false;
        protected List<Point> m_aPoint = new List<Point>();

        /// <summary>
        /// 
        /// </summary>
        public Corner()
            : this(10, false)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nThreshold"></param>
        /// <param name="_bMax"></param>
        public Corner(int _nThreshold, bool _bMax)
        {
            m_nThreshold = _nThreshold;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_corner"></param>
        internal Corner(Corner _corner)
        {
            this.m_nThreshold = _corner.m_nThreshold;
            this.m_bMax = _corner.m_bMax;
        }

        /// <summary>
        /// Get points
        /// </summary>
        public List<Point> Points
        {
            get { return m_aPoint; }
        }

        /// <summary>
        /// Gets or sets the threshold
        /// </summary>
        public int Threshold
        {
            get { return m_nThreshold; }
            set { m_nThreshold = value; }
        }

        /// <summary>
        /// Gets or sets max
        /// </summary>
        public bool Max
        {
            get { return m_bMax; }
            set { m_bMax = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            //First implementation
            UnsafeBitmap bitmap = _bitmap;

            List<Point> aPoint = Definitions.EnvironmentPointList;
            int[] aSum = new int[8];
            Color[] aColor = new Color[8];

            int nValue = 0;

            Graphics graphics = Graphics.FromImage(_bitmap.InternalBitmap);

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;
            for (y = 1; y < nHeight - 1; y++)
            {
                for (x = 1; x < nWidth - 1; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    for (int i = 0; i < 8; i++)
                    {
                        aColor[i] = bitmap.GetPixel(x + aPoint[i].X, y + aPoint[i].Y);
                        aSum[i] = (int)ColorFunctions.Distance2(color, aColor[i]);
                    }

                    Array.Sort(aSum);

                    nValue = m_bMax ? aSum[0] : aSum[aSum.Length - 1];
                    if (nValue > m_nThreshold)
                    {
                        m_aPoint.Add(new Point(x, y));
                        graphics.DrawRectangle(Pens.Red, x, y, 3, 3);
                        bitmap.SetPixel(x, y, m_ColorForeground);
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return bitmap;
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Corner(this);
        }
    }
}
